//
//  CustomNewFolderViewController.m
//  我的图书馆
//
//  Created by 孙  路 on 15-1-6.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CustomNewFolderViewController.h"

#import "LibraryViewController.h"

#import "CustomModel.h"

//#import "EditDataBase.h"//调用数据库
#import "DetailModel.h"
#import "DetailBace.h"
//#import "EditModel.h"//创建model

#define kBlueColor [UIColor colorWithRed:129/255.0 green: 198/255.0 blue: 221/255.0 alpha: 1.0]
@interface CustomNewFolderViewController ()

@end

@implementation CustomNewFolderViewController
- (void)dealloc
{
    [_tableArray release];
    [_tableV release];
    [_aView release];
    [_field release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];

        self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
        [_tableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"folderCell"];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        [self.view addSubview:_tableV];

        [self createTabBar];
        [_tableV release];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNavigation];
}
#pragma mark -
#pragma mark 先读取文件夹
- (void)readFolderFromDateBace
{
    if ([_tableArray count] != 0) {
        _tableArray = [DetailBace searchAllEditByFolderName:@"EditFolder"];
        [_tableV reloadData];
    }
}
#pragma mark -
#pragma mark 完成编辑文件夹(nav)
- (void)createNavigation
{
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    [aView setBackgroundColor:kBlueColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setFrame:CGRectMake(315, 20, 30, 40)];
    [button addTarget:self action:@selector(backToTheList) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"完成" forState: UIControlStateNormal];
    [aView addSubview:button];

    [self.view addSubview:aView];
    [aView release];
}

- (void)backToTheList//返回列表
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"listBackRightNow" object:self userInfo:nil];
}

#pragma mark -
#pragma mark 编辑文件夹选项(自定义tabBar)
- (void)createTabBar
{
    NSArray *array = [NSArray arrayWithObjects:@"建文件夹", @"重命名", @"删除", nil];
    NSArray *imageArray = [NSArray arrayWithObjects:@"iconfont-xinjianwenjianjia.png", @"iconfont-shuaxin.png", @"iconfont-shanchu.png", nil];

    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 60, self.view.frame.size.width, 60)];

    [bView setBackgroundColor:[UIColor whiteColor]];
    for (int i = 0; i < 3; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setFrame:CGRectMake((self.view.frame.size.width / 10.0f - 17) + self.view.frame.size.width / 5.0f * i * 2, 10, 30, 30)];
        button.tag = 10 + 1 + i;
        [button addTarget:self action:@selector(editFolder:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:[imageArray objectAtIndex:i]] forState:UIControlStateNormal];
        [bView addSubview:button];

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width / 10.0f - 17) + self.view.frame.size.width / 5.0f * i * 2 - 4, 40, 40, 20)];
        [label setText:[array objectAtIndex:i]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setFont:[UIFont systemFontOfSize:10]];
        [bView addSubview:label];
        [label release];

    }
    [self.view addSubview:bView];
    [bView release];
}
#pragma mark -
#pragma mark 添加文件夹,重命名,删除
- (void)editFolder:(UIButton *)button
{

    if (button.tag == 11) {//创建文件夹并命名

        [UIView animateWithDuration:0.0f animations:^{

            self.aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            _aView.alpha = 0.5f;
            [_aView setBackgroundColor:[UIColor grayColor]];

            self.field = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 30)];
            _field.layer.cornerRadius = 5;
            _field.alpha = 1.0f;
            _field.backgroundColor = [UIColor whiteColor];
            _field.placeholder = @"请输入文件名";
            [_aView addSubview:_field];
            [_field release];

            UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
            [cancelButton setFrame:CGRectMake(20 , 150, self.view.frame.size.width / 2 - 21, 30)];
            [cancelButton setBackgroundColor:[UIColor whiteColor]];
            [cancelButton addTarget:self action:@selector(cancelClickBack) forControlEvents:UIControlEventTouchUpInside];
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            cancelButton.layer.cornerRadius = 3;
            cancelButton.layer.masksToBounds = YES;
            [_aView addSubview:cancelButton];

            UIButton *defineButton = [UIButton buttonWithType:UIButtonTypeSystem];
            [defineButton setFrame:CGRectMake(self.view.frame.size.width / 2 + 1, 150, self.view.frame.size.width / 2 - 19, 30)];
            [defineButton setBackgroundColor:[UIColor whiteColor]];
            [defineButton setTitle:@"确定" forState:UIControlStateNormal];
            [defineButton addTarget:self action:@selector(createNewFolder) forControlEvents:UIControlEventTouchUpInside];
            defineButton.layer.cornerRadius = 3;
            defineButton.layer.masksToBounds = YES;
            [_aView addSubview:defineButton];
            [self.view addSubview:_aView];
            [_aView release];
        }];
        [_tableV reloadData];

    }else if (button.tag == 12) {//重命名指定文件夹

        if (_tempFolderNum != 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入文件夹名字" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
            [alert show];
            [alert release];
        }

    }else if (button.tag == 13) {//删除指定文件夹(不能成批量)

        if (_tempFolderNum != 0) {
            CustomModel *cus = [_tableArray objectAtIndex:_tempFolderNum - 1];
//            [DetailBace deleteTableByFolderName:cus.folderTitle];
            [_tableArray removeObjectAtIndex:_tempFolderNum - 1];
            _tempFolderNum = 0;
            [_tableV reloadData];
        }
    }
}

- (void)createNewFolder//alert点击事件----创建新的文件夹,并且对应的数据库
{
    if ([_field.text length] != 0) {
        DetailModel *model = [[DetailModel alloc] init];
//        model.allFolderName = _field.text;

//        [DetailBace createNewTableFolderName:model.allFolderName];//创建数据库
        //[DetailBace insertIntoDetailWithDetailModel:<#(DetailModel *)#>];

        [_tableArray addObject:model];
        [UIView animateWithDuration:0.0f animations:^{
            [_aView setFrame:CGRectMake(2 * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        }];//确定后相当于文件夹创建完成,然后退出alert

        [_tableV reloadData];
        [_field resignFirstResponder];//键盘回收
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"文件夹名字不能为空" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
//    if ([_field.text length] != 0) {
//        DetailModel *model = [[DetailModel alloc] init];
//        model.allFolderName = _field.text;
//
//        [DetailBace createNewTableFolderName:model.allFolderName];//创建数据库
//        [DetailBace insertIntoDetailWithDetailModel:model byFolderName:@"EditFolder"];
//
//        [_tableArray addObject:model];
//        [UIView animateWithDuration:0.0f animations:^{
//            [_aView setFrame:CGRectMake(2 * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
//        }];//确定后相当于文件夹创建完成,然后退出alert
//
//        [_tableV reloadData];
//        [_field resignFirstResponder];//键盘回收
//    }else{
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"文件夹名字不能为空" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
//        [alert show];
//        [alert release];
//    }

}

- (void)cancelClickBack//点击取消返回
{
    [UIView animateWithDuration:0.0f animations:^{
        [_aView setFrame:CGRectMake(- self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }];

}
/*
 1. 点击alert按钮触发事件
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([_tableArray count] != 0) {
        UITextField *field = [alertView textFieldAtIndex:0];
        DetailModel *model = [_tableArray objectAtIndex:_tempFolderNum - 1];
//        [DetailBace renamingTableFolderName:model.allFolderName newFolderName:field.text];
//        model.allFolderName = field.text;
        [_tableV reloadData];
    }
//    if ([_tableArray count] != 0) {
//        UITextField *field = [alertView textFieldAtIndex:0];
//        DetailModel *model = [_tableArray objectAtIndex:_tempFolderNum - 1];
//        [DetailBace renamingTableFolderName:model.allFolderName newFolderName:field.text];
//        model.allFolderName = field.text;
//        [_tableV reloadData];
//    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _tempFolderNum = indexPath.row + 1;
}
#pragma mark -
#pragma mark 添加文件夹状态(图标及名字显示)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"folderCell"];

    if ([_tableArray count] != 0) {
//        DetailModel *model = [_tableArray objectAtIndex:indexPath.row];
//        [cell.imageView setImage:[UIImage imageNamed:@"iconfont-iconfontwenjianjia.png"]];
        DetailModel *model = [_tableArray objectAtIndex:indexPath.row];
        [cell.imageView setImage:[UIImage imageNamed:@"iconfont-iconfontwenjianjia.png"]];
//        cell.textLabel.text = model.allFolderName;
        return cell;
    }
    return cell;
}
#pragma mark -
#pragma mark cell样式编辑(处于编辑状态, 获得编辑样式, 提交样式)
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath == 0) {
//        return UITableViewCellEditingStyleInsert;
//    }else
//        return UITableViewCellEditingStyleDelete;
//}
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [_tableArray removeObjectAtIndex:indexPath.row];
//        [_tableV reloadData];
//    }else if (editingStyle == UITableViewCellEditingStyleInsert){
//        [_tableArray addObject:@"1"];
//        [_tableV reloadData];
//    }
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
